﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataInput;
using Slaks.Graphs.GraphBuilder;
using Slaks.Graphs.Graphs;

namespace Slaks.Graphs.Analysis
{
    public class Measures
    {
        public const String measuresParam = "Measures";
        private List<Object> allMeasures = new List<object>();
        private TaskType task;

        public Measures ( TaskType task )
        {
            this.task = task;
        }

        public void addMeasure ( String measureName )
        {
            Object currentMeasure = null;
            if ( task == TaskType.Categorization)
                currentMeasure = CategorizationMeasureFactory.Instance.GetInstance(measureName);
            else
                currentMeasure = ClusteringMeasureFactory.Instance.GetInstance(measureName);  
            allMeasures.Add(currentMeasure);
        }

        public void setMeasures ( String [] measures )
        {
            allMeasures.Clear();
            for ( int i = 0; i < measures.Length; i++ )
                addMeasure(measures [ i ]);
        }

        public Dictionary<String, Object> evaluate ( params Object [] parameters )
        {
            Dictionary<String, Object> returnVal = new Dictionary<string, object>();
            for ( int i = 0; i < allMeasures.Count; i++ )
            {
                if ( task == TaskType.Categorization )
                {
                    CategorizationMeasureBase measure = (CategorizationMeasureBase) allMeasures[i];
                    returnVal.Add( measure.getMeasureName(), measure.evaluate((Dictionary<Graph, string>) parameters[0], (Category.CategoriesFile) parameters[1]));
                }
                else
                {
                    ClusteringMeasureBase measure = (ClusteringMeasureBase) allMeasures[i];
                    returnVal.Add( measure.getMeasureName(), measure.evaluate((GraphList[])parameters[0], (Category.CategoriesFile)parameters[1]));
                }
            }
            return returnVal;
        }

        public virtual List<Field> GetDynamicFields(IRepresentation BT)
        {
            List<Field> fields = new List<Field>();
            for (int i = 0; i < allMeasures.Count; i++)
            {
                List<Field> f;
                if (allMeasures[i] is CategorizationMeasureBase)
                {
                    CategorizationMeasureBase measure = (CategorizationMeasureBase) allMeasures[i];
                    f = measure.GetDynamicFields(BT);
                }
                else
                {
                    ClusteringMeasureBase measure = (ClusteringMeasureBase) allMeasures[i];
                    f = measure.GetDynamicFields(BT);
                }
                for ( int j = 0; j < f.Count; j++ )
                    fields.Add(f [ j ]);
            }
            return fields;
        }

        public virtual void SetDynamicValues(IEnumerable<Field> fields)
        {
            for ( int i = 0; i < allMeasures.Count; i++ )
            {
                if (task == TaskType.Categorization)
                {
                    CategorizationMeasureBase measure = (CategorizationMeasureBase)allMeasures[i];
                    measure.SetDynamicValues(fields);
                }
                else
                {
                    ClusteringMeasureBase measure = (ClusteringMeasureBase)allMeasures[i];
                    measure.SetDynamicValues(fields);
                }
            }
        }
    }
}
